package com.athuahua.r2dbc.controller;

import com.athuahua.r2dbc.entity.TAuthor;
import com.athuahua.r2dbc.entity.TAuthorVo;
import com.athuahua.r2dbc.entity.TBook;
import com.athuahua.r2dbc.repositories.AuthorRepositories;
import com.athuahua.r2dbc.repositories.BookRepositories;
import jakarta.annotation.Resource;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/12/2 0002
 * @Time: 18:16
 * @Description:
 */
@RestController
@RequestMapping
public class BController {

    @Resource
    private AuthorRepositories authorRepositories;

    @Resource
    private BookRepositories bookRepositories;

    @Resource
    private DatabaseClient databaseClient;

    //单表简单查询 Mybatis-plus + JAP
    @GetMapping("test3")
    public Flux<TAuthor> test3() {

        authorRepositories.findAllById(1L)
                .subscribe(tAuthor -> System.out.println("tAuthor = " + tAuthor));

        System.out.println("--------------");

        authorRepositories.findAllByIdAndNameLike(1L, "zhang%")
                .subscribe(tAuthor -> System.out.println("tAuthor = " + tAuthor));

        return authorRepositories.findAllByNameLike("zhang%");
    }

    //多表复杂查询 一对一

    @GetMapping("test4")
    public Mono<TBook> test4() {
        /**
         * 添加转化器后所有查询都会用这个转化器，解决方法
         * 新建一个VO 和一个仓库使用
         * 在转化器中使用 source.getMetadata().contains(“列名”) 进行检测
         **/
        bookRepositories.findById(3L).subscribe(tBook -> System.out.println("tBook = " + tBook));


        return bookRepositories.MyfindBookAndAuthor(2L);
    }

    //多表复杂查询2 一对多

    /**
     * 使用 bufferUntilChanged 需要数据已经按照分组排好了序
     * 或者使用sql中的groupby 进行分组
     * @return
     */
    @GetMapping("test5")
    public Flux<TAuthorVo> oneToN(){
//        Flux.just(1,2,3,4,5)
//                //.buffer(2)//自带分组
//                .bufferUntilChanged(integer -> integer%4 == 0) //直到xxx停止缓存 , 条件发生变化结束分组
//                .subscribe(list -> System.out.println("list = " + list));
//        ;


        Flux<TAuthorVo> map = databaseClient.sql("select t.id tid, t.name , b.* from t_author t left join t_book b on t.id = b.author_id")
                .fetch()
                .all()
                .bufferUntilChanged(rowMap -> Long.parseLong(rowMap.get("tid").toString()))
                .map(list -> {
                    //list<map>
                    TAuthorVo tAuthorVo = new TAuthorVo();
                    Map<String, Object> one = list.get(0);
                    tAuthorVo.setId(Long.parseLong(one.get("tid").toString()));
                    tAuthorVo.setName(one.get("name").toString());

                    //查到的全部数据
                    List<TBook> list1 = list.stream().map(ele -> {
                        TBook tBook = new TBook();
                        if (ele.get("id") == null) {
                            return null;
                        }
                        Long id = Long.parseLong(ele.get("id").toString());
                        String title = ele.get("title").toString();
                        Long author_id = Long.parseLong(ele.get("author_id").toString());


                        tBook.setId(id);
                        tBook.setAuthorId(author_id);
                        tBook.setTitle(title);
                        return tBook;
                    }).toList();

                    tAuthorVo.setBooks(list1);


                    return tAuthorVo;
                });
        //map.subscribe(tAuthorVo -> System.out.println("tAuthorVo = " + tAuthorVo));

        return map;
    }

}
